/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { message } from 'antd';
import type { ActionType } from '@ant-design/pro-table';
import { BasePriceItem } from '../../../index.d';
import ProForm, { ModalForm, ProFormText, ProFormRadio } from '@ant-design/pro-form';
import { saveCarryPrice } from '@/services/jdk/register';
import { getPriceRender, getPriceStorage } from '@/components/_pages/jdk/addLeague/_utils';
import moment from 'moment';

interface RefuseModalProps {
  children: JSX.Element;
  tableRef: ActionType | any;
  item: BasePriceItem;
  user_id: string | string[];
}

const RefuseModal: React.FC<RefuseModalProps> = ({ item, user_id, children, tableRef }) => {
  const {
    brand_name,
    admin_base_price_status,
    admin_base_price_expire_at,
    admin_base_price,
    brand,
  } = item;
  const [transport, setTransport] = useState<boolean>(false);
  const [overdue, setOverdue] = useState<boolean>(false);
  const [status, setStatus] = useState<boolean>(false);
  useEffect(() => {
    setStatus(!!admin_base_price_status);
  }, [admin_base_price_status]);
  useEffect(() => {
    const overdue_type = moment(admin_base_price_expire_at) > moment(new Date());
    setOverdue(overdue_type);
  }, [admin_base_price_expire_at]);
  useEffect(() => {
    setTransport(brand === 'kb' || brand === 'jfkp');
  }, [brand]);
  const onChange = (values: any) => {
    const { target: { value = 0 } = {} } = values;
    setStatus(!!value);
  };

  return (
    <ModalForm
      title='温馨提示'
      trigger={children}
      width={480}
      layout='horizontal'
      modalProps={{
        destroyOnClose: true,
        onCancel: () => setStatus(!!admin_base_price_status),
      }}
      onFinish={async (values) => {
        const { base_price, status: status_, expire_type } = values;
        const config: {
          brand: string;
          user_id: string | string[];
          status: string;
          base_price?: number;
          expire_type: string;
        } = {
          brand,
          user_id,
          status: status_,
          expire_type,
        };
        if (base_price) {
          config.base_price = getPriceStorage(base_price);
        }
        if (brand == 'jfkp') {
          config.status = '1';
        }
        const { code, msg } = await saveCarryPrice({ config });
        if (code == 0) {
          message.success('操作成功');
          tableRef.current?.reload();
          return true;
        } else {
          message.error(msg);
          return false;
        }
      }}
      initialValues={{
        status: admin_base_price_status,
        base_price: getPriceRender(Number(admin_base_price)),
      }}
    >
      {transport ? (
        <ProFormText
          label={brand_name}
          name='base_price'
          placeholder='请输入金额'
          allowClear
          getValueFromEvent={(e) => {
            return e.target.value.replace(/\s+/gi, '');
          }}
          rules={[{ required: true, message: '请输入金额' }]}
          addonAfter={<div>元/单</div>}
        />
      ) : (
        <>
          <ProFormRadio.Group
            name='status'
            options={[
              { label: '使用', value: 1 },
              { label: '不使用', value: 0 },
            ]}
            fieldProps={{
              onChange,
            }}
          />
          {status ? (
            <>
              <ProForm.Group>
                <ProFormText
                  label={brand_name}
                  name='base_price'
                  placeholder='请输入金额'
                  allowClear
                  getValueFromEvent={(e) => {
                    return e.target.value.replace(/\s+/gi, '');
                  }}
                  rules={[{ required: true, message: '请输入金额' }]}
                  addonAfter={<div>元/单</div>}
                />
              </ProForm.Group>
              {admin_base_price_expire_at && overdue ? (
                <ProFormText
                  readonly
                  label='有效期'
                  fieldProps={{
                    value: `${admin_base_price_expire_at}（运力手续费生效时开始计算）`,
                  }}
                />
              ) : (
                <ProFormRadio.Group
                  name='expire_type'
                  label='设置有效期'
                  initialValue='1m'
                  options={[
                    {
                      label: '1个月',
                      value: '1m',
                    },
                    {
                      label: '3个月',
                      value: '3m',
                    },
                    {
                      label: '6个月',
                      value: '6m',
                    },
                    {
                      label: '无限期',
                      value: 'undated',
                    },
                  ]}
                />
              )}
            </>
          ) : null}
        </>
      )}
    </ModalForm>
  );
};

export default RefuseModal;
